<template>
  <l-layout class="l-tab-page" v-show="!lr_loadPage">
    <l-panel>
      <template #toolLeft>
        <div class="l-panel--item">
          <el-input
            :placeholder="$t('请输入关键字查询')"
            v-model="searchWord"
            @keyup.enter.native="hanleSearch"
            size="mini"
          >
            <el-button slot="append" icon="el-icon-search" @click="hanleSearch"></el-button>
          </el-input>
        </div>
      </template>
      <template #toolRight>
        <l-tool-btns @click="handleAdd()">
          <l-excel-btns></l-excel-btns>
        </l-tool-btns>
      </template>
      <l-table
        :loading="tableLoading"
        :columns="lr_getPageColumns(columns)"
        :dataSource="tableShowData"
        row-key="f_Id"
        :isPage="true"
        :pageTotal="tableTotal"
        :tablePage.sync="tableCurrentPage"
        @loadPageData="turnTablePage"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        default-expand-all
      >
        <!--
                <template v-slot:f_TenantType="scope" >
                    {{scope.row.f_TenantType==0 ?"普通租户":"系统平台租户"}}
                </template>
				-->
        <l-table-btns :btns="tableBtns" @click="lr_handleTableBtnClick"></l-table-btns>
      </l-table>
    </l-panel>

    <l-dialog
      :title="formTitle"
      :visible.sync="formVisible"
      @ok="handleSave"
      @close="handleCloseForm"
      @opened="handleOpenedForm"
    >
      <my-form ref="form"></my-form>
    </l-dialog>
  </l-layout>
</template>

<script>
const api = window.$api.report.reportcategory;
import MyForm from "./form";
export default {
  components: {
    MyForm,
  },
  data() {
    return {
      lr_isPage: true,
      // 表格
      columns: [
        { label: "分类名称", prop: "f_Name", minWidth: "120" },
        { label: "备注", prop: "f_Description", minWidth: "120" },
      ],
      tableBtns: [
        { prop: "Edit", label: "编辑" },
        { prop: "Delete", label: "删除" },
      ],
      tableLoading: false,
      searchWord: "",
      tableShowData: [],
      tableData: [],

      formVisible: false,
      formTitle: "",
      formEditRow: null,
      formEdit: false,

      //分页
      tableTotal: 0,
      tablePageSize: 50,
      tableCurrentPage: 1,
      keyWord: "",
    };
  },
  mounted() {
    //var data = await this.$awaitWraper(api2.getTenantList());
    this.$nextTick(() => {
      //this.loadTable();
      this.loadTablePageData();
    });
    //console.log(this);
  },
  computed: {},
  methods: {
    lr_apiTree(data) {
      data = data || [];
      return this.$toTree(data, "f_Id", "f_ParentId", "f_Id", "f_Name");
    },

    loadTablePageData(isNotFirst) {
      if (isNotFirst) {
        this.tableCurrentPage = 1;
        this.searchWord = "";
        this.tableData = [];
      }
      this.tableLoading = true;
      let queryData = {
        rows: this.tablePageSize,
        page: this.tableCurrentPage,
        keyword: this.searchWord,
        f_EnabledMark: 1,
      };
      api
        .getPage(queryData)
        .then((res) => {
          const data = this.$deepClone(res.data.data);
          this.tableData = this.lr_apiTree(data.rows);
          //console.log(this.tableData);
          this.tableTotal = data.records;
          this.tableShowData = this.tableData;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableData = [];
          this.tableLoading = false;
        });
    },
    turnTablePage({ rows }) {
      this.tablePageSize = rows;
      this.loadTablePageData(true);
    },
    hanleSearch() {
      this.loadTablePageData();
    },

    // 表单部分
    handleAdd() {
      this.formEdit = false;
      this.showForm("新增分类");
    },
    handleEdit($index, row) {
      this.formEdit = true;
      this.formEditRow = row;
      this.showForm("编辑分类信息");
    },
    handleDelete($index, row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          api.remove(row.f_Id).then(() => {
            this.tableShowData.splice($index, 1);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async handleSave(showLoading, hideLoading) {
      showLoading();
      if (!(await this.$refs.form.validateForm())) {
        hideLoading();
        return;
      }
      const formData = this.$refs.form.getForm();
      if (this.formEdit) {
        // 修改数据
        api
          .update(formData.f_Id, formData)
          .then(() => {
            hideLoading();
            this.formVisible = false;
            this.$message({
              type: "success",
              message: "更新成功!",
            });
            this.loadTablePageData(true);
          })
          .catch(() => {
            hideLoading();
          });
      } else {
        // 新增数据
        api
          .add(formData)
          .then(() => {
            hideLoading();
            this.formVisible = false;
            this.$message({
              type: "success",
              message: "添加成功!",
            });
            this.loadTablePageData(true);
          })
          .catch(() => {
            hideLoading();
          });
      }
    },
    handleOpenedForm() {
      if (this.formEdit) {
        this.$refs.form.setForm(this.formEditRow);
      }
    },
    handleCloseForm() {
      this.$refs.form.resetForm();
    },
    showForm(text) {
      this.formTitle = text;
      this.formVisible = true;
    },
  },
};
</script>
